let grammar_ll1=require("./class/grammar_ll1");

exports.ll_1 = function (target_text){
    let data=new grammar_ll1();
    data.init_lex(target_text);
    if(E(data)){
        data.log_collector();
        return data.return_collector();
    }else{
        return ["有错误"];
    }

};
let E=function(data){
    switch (data.get_inn()){
        case '(':
        case 'id':

            if(T(data)){
                if(E1(data)){
                    //console.log('success');
                    data.push_collector('E->TE\'');
                    return true;
                }else{
                    //console.log('error');
                    return false;
                }
            }else{
                //console.log('error');
                return false;
            }


            break;
        default:
            //console.log('error');
            return false;
    }

};
let T=function(data){
    switch (data.get_inn()){
        case 'id':
        case '(':
            if(F(data)){
                if(T1(data)){
                    //console.log('success');
                    data.push_collector('T->FT\'');
                    return true;
                }else{
                    //console.log('error');
                    return false;
                }
            }else{
                //console.log('error');
                return false;
            }
            break;
        default:
            //console.log('error');
            return false;
    }

};
let E1=function(data){
    if(match(data,'+')){
        if(T(data)){
            if(E1(data)){
                //console.log('success');
                data.push_collector('E\'->+TE\'');
                return true;
            }else{
                //console.log('error');
                return false;
            }
        }else{
            //console.log('error');
            return false;
        }
    }else{
        //console.log('success');
        data.push_collector('E\'->Epsilon');
        return true;
    }
};
let T1=function (data) {
    if(match(data,'*')){
        if(F(data)){
            if(T1(data)){
                //console.log('success');
                data.push_collector('T\'->*FT\'');
                return true;
            }else{
                //console.log('error');
                return false;
            }
        }else{
            //console.log('error');
            return false;
        }
    }else{
        //console.log('success');
        data.push_collector('T\'->Epsilon');
        return true;
    }
};
let F=function (data) {
    if(match(data,'(')){
        if(E(data)){
            if(match(data,')')){
                //console.log('success');
                data.push_collector('F->(E)');
                return true;
            }else{
                //console.log('error');
                return false;
            }
        }else{
            //console.log('error');
            return false;
        }
    }else if(match(data,'id')){
        data.push_collector('F->id');
        //console.log('success');
        return true;
    }else{
        //console.log('error');
        return false;
    }

};
let match = function(data,inn){
    if(inn===data.get_inn()){
        data.nextlex();
        return true;
    }else{
        //console.log("error");
        return false;
    }
};
